package com.heqq.zixi;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

/**
 * @program: TestSelenium
 * @description: selenium+java模拟键盘鼠标操作
 * @author: heqq
 * @create: 2018-09-06 10:58
 **/
public class KeyboardMouseOperation {

    @BeforeClass
    public void chromedrivers(){
        String chromedriver = "c:\\google\\chromedriver.exe";

        System.setProperty("webdriver.gecko.driver", chromedriver);


    }

   // private static  WebDriver driver = new ChromeDriver();


    //模拟鼠标点击操作
    @Test
    public void clickKey(){
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com");
        Actions actions = new Actions(driver);
        //actions.keyDown(Key.按钮);是点击该按钮，但并未松开，例如：
        actions.keyDown(Keys.CONTROL);
        actions.keyDown(Keys.SHIFT);
        actions.keyDown(Keys.ALT);
        actions.keyDown(Keys.ENTER);
        //actions.keyUp(Keys.按钮);是点击过后松开该按钮，例如：
        actions.keyUp(Keys.ENTER);
        actions.keyUp(Keys.ALT);
        actions.keyUp(Keys.SHIFT);
        actions.keyUp(Keys.CONTROL);
    }
    //模拟键盘输入操作和鼠标点击操作
    @Test
    public void actionTest(){

        WebDriver driver = new ChromeDriver();

        //智能等待10s
        driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
        driver.get("http://www.baidu.com");
        By inputBox = By.id("kw");
        By searchButton = By.id("su");
        //实例化Actions
        Actions actions = new Actions(driver);
        //通过actions模拟键盘使用perform方法输入关键字heqq到输入框
        actions.sendKeys(driver.findElement(inputBox),"heqq").perform();
        //模拟鼠标移动到按钮
        actions.moveToElement(driver.findElement(searchButton)).perform();
        //点击操作
        actions.click().perform();
        try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        driver.quit();
    }

    //模拟鼠标右键点击
    @Test
    public void rightClickMouse(){
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.baidu.com");
        By ids = By.id("kw");
        Actions actions = new Actions(driver);
        actions.contextClick(driver.findElement(ids)).perform();

    }
    //在指定的元素上面悬浮
    @Test
    public void roverOnElement(){
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.baidu.com");
        driver.manage().window().maximize();
        WebElement link = driver.findElement(By.linkText("更多产品"));
        System.out.println(link);
        Actions actions = new Actions(driver);
        actions.moveToElement(link).perform();

    }
    //模拟鼠标左键点击和释放
    @Test
    public void mouseClickAndRelease() throws InterruptedException {
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.baidu.com");
        driver.manage().window().maximize();
        By inputBox = By.id("kw");
        WebElement searchButton = driver.findElement(By.id("su"));
        Actions actions = new Actions(driver);
        actions.sendKeys(driver.findElement(inputBox),"2222").perform();
        //点击鼠标左键但不松开
        actions.clickAndHold(searchButton).perform();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //松开鼠标左键
        actions.release(searchButton).perform();
        Thread.sleep(2000);
        driver.quit();
    }



}
